package com.itheima.login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //过滤器全局配置了请求和响应乱码问题，不需要处理乱码
        //获取前端参数
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        //创建User对象封装数据
        User user = new User(name, password);
        System.out.println("user = " + user);
        //将user数据存入Session域中
        request.getSession().setAttribute("user",user);
        //登录成功，重定向到hack页面
        response.sendRedirect("/hack.html");
    }
}